import { reactive, inject, provide, toRaw } from 'vue';
import { ConsoleLogger } from '@/utils/debug-log';
import { ESubmitType, HomeWorkList, QueryHomeWork, CurrentSubject } from '../types/jobs/homeWork';
import { getHomeWorkList } from '../api/homeWork';
import { ECommitStatus, ERejected, StdHomeWork, SubjectInfo } from '../types/jobs';
import { AnswersID, AnswersTestID, StuID } from '../types';
import { getCHNumber } from '@/utils/chString';
import { ReqJobCorrectParam, ReqRevisalParam } from '../types/jobs/jobCorrect';
import { correctSave, correctRevise } from '../api/editor/index';
import { BaseResponse } from '../api/index';
import { router } from '@/project/correction/phoneOa/router';

const console = new ConsoleLogger('学生作业');
const useHomeWorksSymbol = Symbol('useHomeWorks store');

export interface IHomeWorksState {
  homeWorkList?: HomeWorkList; //
  aCList?: Array<StdHomeWork>; // 已批改学员列表
  nCList?: Array<StdHomeWork>; // 未批改学员列表
  allList?: Array<StdHomeWork>; // 已排好序的作业列表
}

export interface IUseHomeWorksStoreFn {
  (): IUseHomeWorksStore;
  __CurrentStore?: IUseHomeWorksStore;
}

export interface IUseHomeWorksStore {
  state: IHomeWorksState;
  init: () => void;
  toCorrectSave: (param: ReqJobCorrectParam) => Promise<BaseResponse<{}>>;
  toCorrectRevise: (param: ReqRevisalParam) => Promise<BaseResponse<{}>>;
}
export const loginStatus = { HomeWorks: {}, isLoginStatus: false };
/**
 *  * useHomeWorksStore 用户hooks.
 * @function
 * @params {IHomeWorksState} userState - 用户信息.
 * @returns store extend IUseHomeWorksStore
 */
export const useHomeWorksProvide: () => IUseHomeWorksStore = () => {
  const state = reactive<IHomeWorksState>({
    homeWorkList: undefined,
    aCList: [],
    nCList: [],
    allList: []
  });

  const objSort = (property: string) => {
    return function (a: any, b: any) {
      return a[property] - b[property];
    };
  };
  const init = async () => {
    let routerParams: QueryHomeWork = { work_type: 1, class_id: 0, work_id: 0, plan_id: 0 };
    if (router.currentRoute.value.query.class_id) {
      routerParams = {
        work_type: 1,
        class_id: Number(router.currentRoute.value.query.class_id),
        work_id: Number(router.currentRoute.value.query.work_id),
        plan_id: Number(router.currentRoute.value.query.plan_id)
      };
      window.localStorage.setItem('routerParams', JSON.stringify(routerParams));
    } else {
      routerParams = JSON.parse(window.localStorage.getItem('routerParams') || '');
    }
    if (!routerParams.class_id) {
      return;
    }
    const data = await getHomeWorkList(routerParams);
    state.homeWorkList = data.data;
    let homeWorkList =
      data.data &&
      data.data.list
        .map((item) => {
          return { ...item, commit_id: item.commit.commit_id };
        })
        .sort(objSort('commit_id'));

    state.aCList = homeWorkList.filter((item) => {
      return item.timeout_status === 3;
    });
    state.nCList = homeWorkList.filter((item) => {
      return item.timeout_status < 3;
    });
    state.allList = state.nCList.concat(state.aCList);
  };

  const toCorrectSave = async (param: ReqJobCorrectParam) => {
    const data = await correctSave(param);
    return data;
  };

  const toCorrectRevise = async (param: ReqRevisalParam) => {
    const data = await correctRevise(param);
    return data;
  };

  const store: IUseHomeWorksStore = {
    state,
    init,
    toCorrectSave,
    toCorrectRevise
  };

  provide(useHomeWorksSymbol, { ...store });

  return store;
};

/**
 *  * useHomeWorksStore 用户hooks.
 * @function
 * @returns store extend IUseHomeWorksStore
 */
export const useHomeWorksStore: IUseHomeWorksStoreFn = () => {
  const store: IUseHomeWorksStore = inject(useHomeWorksSymbol) as IUseHomeWorksStore;
  return store;
};
